package logic.nodes.damagable.shield;

import logic.nodes.damagable.DamagableNode;

import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.TriMesh;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.MaterialState.MaterialFace;
import com.jme.system.DisplaySystem;

public abstract class ShieldEffectObject extends Node {
	
	private static final long serialVersionUID = 1L;
	
	private ColorRGBA shieldColor;
	private ShieldsController shieldsController;
	
	protected MaterialState matState;
	protected TriMesh object;
	protected DamagableNode node;
	protected float maxDist;
	protected Vector3f location;
	protected Shields shields;
	
	public ShieldEffectObject(DamagableNode node, Shields shields, Vector3f location, float maxDist) {
		super("shield effect object of " + node.getName());
		this.node = node;
		this.maxDist = maxDist;
		this.location = location;
		this.shields = shields;
		
		assert(location != null);
		assert(node != null);
		assert(maxDist > 0f);
		
		shieldColor = new ColorRGBA(0f, 0f, 1f, 1f);
		shieldsController = new ShieldsController(this);
		addController(shieldsController);
		
		DisplaySystem display = DisplaySystem.getDisplaySystem();
		Renderer renderer = display.getRenderer();
		
		object = getObject();
		attachChild(object);
		
		matState = renderer.createMaterialState();
		matState.setDiffuse(shieldColor);
		matState.setShininess(0f);
		matState.setMaterialFace(MaterialFace.FrontAndBack);
		matState.setEnabled(true);
		object.setRenderState(matState);
		
		object.setLightCombineMode(LightCombineMode.Off);
		object.setCastsShadows(false);
		object.updateRenderState();
	}
	
	protected abstract TriMesh getObject();
	
	public abstract void setAlpha(float alpha);
	
	public void disable() {
		removeFromParent();
		removeController(shieldsController);
		shields.removeImpact(this);
	}
	
	public Vector3f getLocation() { return location; }
}